<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 15.05.12
 * Time: 14:18
 * To change this template use File | Settings | File Templates.
 */
class Backend_Model_DbTable_BeUsers extends UcsMvc_AbstractDbTable
{
    public $_name    = 'be_users';

    public $_passwordColumn = 'be_users_password';

    public $_usernameColumn = 'be_users_email';

    public function getUsersBy($companyId){
        $select = $this->select()
                    ->where($this->_name.'_deleted = 0')
                    ->where('bus_company_nid = ?', $companyId);

        $result = $this->fetchAll($select)->toArray();

        return $result;
    }

    /**
     * @param $id
     * @return bool - returns true if 'deleted'  = 0
     *              - returns false if 'deleted' = 1 OR id not found
     */
    public function deleted($id){
        $select = $this->select()
                        ->where($this->_primary[1].' = '.$id);

        $result = $this->fetchAll($select)->toArray();

        if(empty($result)){
            return true;
        }elseif($result[0][$this->_name.'_deleted'] == '1'){
            return true;
        }

        return false;
    }

    /**
     * prueft, ob emailadresse bereits vergeben ist
     *
     * @param $email
     *
     * @return bool
     */
    public function exists($email){
        $exists = false;

        $select = $this->select()->where($this->_name.'_email = ?', $email);
        $result = $this->fetchAll($select)->toArray();

        if(!empty($result)){
            $exists = true;
        }

        return $exists;
    }

    /**
     * @param $params
     *
     * @return int - inserted id
     *
     * overrrides Standard addRow function in AbstractDbTable
     */
    public function addRow($params){
        $cols = $this->_getCols();
        foreach($params as $pIndex => $p){
            if(!in_array($pIndex, $cols)){
                unset($params[$pIndex]);
            }
        }

        if(in_array($this->_name.'_password', $cols)){
            $params[$this->_name.'_password'] = md5($params[$this->_name.'_password']);
        }

        return $this->insert($params);
    }

    /**
     * compares each given param index with columnnames
     * if there is no match param will be unset
     *
     * @param $params
     * @return bool
     *
     * overrides editRow from parent
     */
    public function editRow($params, $id = null){
        $cols    = $this->_getCols();

        if(isset($params['id'])){
            $id = $params['id'];
        }

        if(!$this->deleted($id)){
            foreach($params as $pIndex => $p){
                if(!in_array($pIndex, $cols)){
                    unset($params[$pIndex]);
                }elseif($this->_name.'_password' == $pIndex){
                    $params[$pIndex] = md5($p);
                }
            }
            $this->update($params, $this->_primary[1]." = $id");

            return true;
        }

        return false;
    }

    public function passwordValid($id, $password){
        $user = self::getUser($id);

        if($user){
            if(md5($password) == $user['password']){
                return true;
            }
        }

        return false;
    }
}
